package pers.zff.web.validate;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import lombok.extern.slf4j.Slf4j;
import pers.zff.web.common.MessageInfo;
import pers.zff.web.dao.UserMapper;
import pers.zff.web.po.UserPo;

@Slf4j
@Component
public class UserValidate {

	@Autowired
	private UserMapper userMapper;

	public MessageInfo userRepeatValidate(UserPo userPo, MessageInfo messageInfo) {
		try {
			UserPo user = userMapper.fetchUserPoByName(userPo.getUserName());
			if (user != null) {
				messageInfo.putError("用户名已存在");
				return messageInfo;
			}
		} catch (Exception e) {
			log.info(e.toString());
			e.printStackTrace();
		}
		return messageInfo;
	}

	public Map<String, Object> sendEmailValidata(UserPo userpo, MessageInfo messageInfo) {
		Map<String, Object> map = new HashMap<>();
		// 根据用户名获取到用户信息
		UserPo userInfo = userMapper.fetchUserPoByName(userpo.getUserName());
		if (userInfo.equals(null)) {
			messageInfo.putError("没有此用户哦");
			map.put("messageInfo", messageInfo);
			return map;
		}
		map.put("messageInfo", messageInfo);
		map.put("userInfo", userInfo);
		return map;
	}

	public Map<String, Object> updatePwdValidata(UserPo userpo, MessageInfo messageInfo) {
		Map<String, Object> map = new HashMap<>();
		// 根据用户名获取到用户信息
		UserPo userInfo = userMapper.fetchUserPoByName(userpo.getUserName());
		if (userInfo.getUserPassword().equals(userpo.getUserPassword())) {
			messageInfo.putError("密码不能和旧密码相同");
			map.put("messageInfo", messageInfo);
			return map;
		}
		if (!userInfo.getRandomText().equals(userpo.getRandomText())) {
			messageInfo.putError("请核对邮件，输入正确的验证码");
			map.put("messageInfo", messageInfo);
			return map;
		}
		map.put("userInfo", userInfo);
		map.put("messageInfo", messageInfo);
		return map;
	}

	public MessageInfo userLoginValidata(UserPo userpo, MessageInfo messageInfo) {
		UserPo user = new UserPo();
		user.setUserName(userpo.getUserName());
		user.setMarkForDelete(false);
		UserPo selectOne = userMapper.selectOne(user);
		if (selectOne == null) {
			messageInfo.putError("没有此用户，请核实账号");
			return messageInfo;
		} else {
			if (!userpo.getUserPassword().equals(selectOne.getUserPassword())) {
				messageInfo.putError("密码错误");
				return messageInfo;
			}
		}
		return messageInfo;
	}

}
